/** 
 * @Title:TODO  
 * @Desription:TODO
 * @CreateDate:2014-5-8   
 * @Version:0.1 
 *    
 */ 

package com.ai.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ai.web.app.AppContext;
import com.ai.web.service.RepairService;
import com.ai.web.util.WorkStatus;
import com.ai.web.vo.AppRepairStatusV;
import com.ai.web.vo.RepairV;
import com.ai.web.vo.ResultModel;

/** 
 * 提供给app的接口
 * @Description: TODO 
 * @date: 2014-5-8
 * 
 */
@Controller
@RequestMapping("/apprepair")
public class AppRepairController extends BaseController {
    private static Logger logger = Logger.getLogger(RepairController.class);
    @Autowired
    private RepairService repairService;
    
    /**
     * 同步工单状态
     * @param 工单号串,以#隔开
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "/synchronous")
    public ResultModel synchronous(@RequestParam(required=false) String repairIds) throws Exception {
        
        ResultModel rm = new ResultModel();
        if(repairIds!=null && !"".equals(repairIds) && !"null".equals(repairIds)) {
            String[] ids = repairIds.split("_");
            List<AppRepairStatusV> list = new ArrayList<AppRepairStatusV>();
            for(String repairId : ids) {
                AppRepairStatusV vo = new AppRepairStatusV();
                String status = "-1";
                String statusName = "工单号不存在";
                RepairV repair = null;
                try {
                	if(repairId!=null && !"".equals(repairId) && !"null".equals(repairId)) {
                		repair = repairService.getRepair(Integer.valueOf(repairId));
                	}
                }catch(Exception e) {
                	//logger.error(e.getMessage(), e);
                }
                if(repair!=null) {
                    status = String.valueOf(repair.getStatus());
                    statusName = WorkStatus.getName(repair.getStatus());
                }
                vo.setId(repairId);
                vo.setStatus(status); 
                vo.setStatusName(statusName);
                list.add(vo);
            }
            rm.setSuccessCode();
            if(list.size()>0) {
                rm.setMsg("成功");
                rm.setData(list);
            }else {
                rm.setFailCode();
                rm.setMsg("失败");
            }
        }else {
            rm.setFailCode();
            rm.setMsg("工单号不能为空");
        }
        return rm;
    }
    
    @ResponseBody
    @RequestMapping(value = "/appversion")
    public ResultModel appversion() throws Exception {
        ResultModel rm = new ResultModel();
        String appVersion = AppContext.getAppVersionByFile();
        if(appVersion!=null && !"".equals(appVersion)) {
            rm.setData(String.valueOf(appVersion));
            rm.setMsg("成功");
            rm.setSuccessCode();
        }else {
            rm.setMsg("失败");
            rm.setFailCode();
        }
        return rm;
    }
    
    
    
}
